﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string script = @"<script>
                function OpenPopup() {
                popup('/Pages/Views/PopupListContacts.aspx');
                }
                function popup(url) {

                var width = 500;
                var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) { newwin.focus() }
                return false;
                }
                </script>";

                loadContactType();    

                if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(),
                        "Mast", script);
                }

                if (Convert.ToInt32(Request.QueryString["ContactId"]) > 0)
                {
                    btAdd.Visible = false;
                    txtContact.ReadOnly = true;
                    LoadContactById();

                }
                else
                {
                    btUpdate.Visible = false;                   
                }
                 
            }           
        }

        private void LoadContactById()
        {
            int id = Convert.ToInt32(Request.QueryString["ContactId"]);
            var biz = new ContactBiz();
            var contact = new Contact();
            contact = biz.getContactDetail(id);
            txtFName.Text = Convert.ToString(contact.FirstName);
            txtSName.Text = Convert.ToString(contact.SurName);
            txtKnown.Text = Convert.ToString(contact.KnowAs);
            txtOffice.Text = Convert.ToString(contact.OfficePhone);
            txtMobile.Text = Convert.ToString(contact.MobilePhone);
            txtHomePhone.Text = Convert.ToString(contact.STHomePhone);
            txtEmail.Text = Convert.ToString(contact.Email);
            txtContact.Text = Convert.ToString(contact.ContactName);
            hdnManagerId.Value = Convert.ToString(contact.ManagerId);

            //var selectedItem = ddlContactType.Items.FindByValue(Convert.ToString(contact.ContactType));
            //if (selectedItem != null)
            //{
            //    ddlContactType.ClearSelection();
            //    selectedItem.Selected = true;
            //}
            ddlContactType.SelectedIndex = Convert.ToInt32(contact.ContactId);
            txtJobRole.Text = Convert.ToString(contact.JobRole);
            txtWork.Text = Convert.ToString(contact.WorkBase);
            txtJobTitle.Text = Convert.ToString(contact.WorkBase);
            ckActive.Checked = Convert.ToBoolean(contact.IsActive);
            
        }
        private void loadContactType()
        {
            var listContact = new List<Contact>();
            var biz = new ContactBiz();
            listContact = biz.getContactType();
            ddlContactType.Items.Clear();
            //ddlContactType.ClearSelection();
            //if (listContact != null)
            //{
            //    ddlContactType.DataSource = listContact;
            //    ddlContactType.DataTextField = "ContactType";
            //    ddlContactType.DataValueField = "ContactId";
            //    ddlContactType.DataBind();   
            //}

            foreach (Contact contact in listContact)
            {
                string id = Convert.ToString(contact.ContactId);
                string name = Convert.ToString(contact.ContactType);
                var itemChild = new ListItem(name, id);
                ddlContactType.Items.Add(itemChild);
            }
            ddlContactType.Items.Insert(0, new ListItem(" --Chose-- ", "0"));
        }
        protected void btAdd_Click(object sender, EventArgs e)
        {
            var biz = new ContactBiz();
            var contact = new Contact
            {                
                ManagerId = Convert.ToInt32(hdnManagerId.Value),
                FirstName = Convert.ToString(txtFName.Text),
                SurName = Convert.ToString(txtSName.Text),
                KnowAs = Convert.ToString(txtKnown.Text),
                OfficePhone = Convert.ToString(txtOffice.Text),
                MobilePhone = Convert.ToString(txtMobile.Text),
                STHomePhone = Convert.ToString(txtHomePhone.Text),
                Email = Convert.ToString(txtEmail.Text),
                ContactType = Convert.ToString(ddlContactType.SelectedItem),
                JobRole = Convert.ToString(txtJobRole.Text),
                WorkBase = Convert.ToString(txtWork.Text),
                JobTitle = Convert.ToString(txtJobTitle.Text),
                IsActive = ckActive.Checked ? true : false
            };
            bool result = biz.addContact(contact);
            ClearText();
        }
       
        protected void btUpdate_Click(object sender, EventArgs e)
        {
            var biz = new ContactBiz();
            int id = Convert.ToInt32(Request.QueryString["ContactId"]);
            var contact = new Contact
            {    
                ContactId = id,
                ManagerId = Convert.ToInt32(hdnManagerId.Value),
                FirstName = Convert.ToString(txtFName.Text),
                SurName = Convert.ToString(txtSName.Text),
                KnowAs = Convert.ToString(txtKnown.Text),
                OfficePhone = Convert.ToString(txtOffice.Text),
                MobilePhone = Convert.ToString(txtMobile.Text),
                STHomePhone = Convert.ToString(txtHomePhone.Text),
                Email = Convert.ToString(txtEmail.Text),
                ContactType = Convert.ToString(ddlContactType.SelectedItem),
                JobRole = Convert.ToString(txtJobRole.Text),
                WorkBase = Convert.ToString(txtWork.Text),
                JobTitle = Convert.ToString(txtJobTitle.Text),
                IsActive = ckActive.Checked ? true : false
            };
            bool result = biz.updateContact(contact);
            if (result == true)
            {
                Response.Redirect("../Views/ListContact.aspx");
            }
        }

        private void ClearText()
        { 
            hdnManagerId.Value = string.Empty;
            txtFName.Text = string.Empty;
            txtSName.Text = string.Empty;
            txtKnown.Text = string.Empty;
            txtOffice.Text = string.Empty;
            txtMobile.Text = string.Empty;
            txtHomePhone.Text = string.Empty;
            txtEmail.Text = string.Empty;
            //ddlContactType.SelectedItem = 
            txtJobRole.Text = string.Empty;
            txtWork.Text = string.Empty;
            txtJobTitle.Text = string.Empty;
            ckActive.Checked = false;
        }
        protected void btClear_Click(object sender, EventArgs e)
        {
            ClearText();
        }
    }
}